package io.jft.doll.model.viewModel

import android.arch.lifecycle.MutableLiveData
import android.text.InputType
import com.g.base.extend.runDataBaseTransition
import com.g.base.extend.switchMap
import com.g.base.viewModel.BaseViewModel
import io.jft.doll.model.bindModel.InputBind
import io.jft.doll.room.database.AppDatabase
import io.jft.doll.room.entity.common.AddressItemCEntity
import io.jft.doll.room.repository.AddressRepository

/**
 * Created by G on 2017/11/17 0017.
 */
class AddressViewModel : BaseViewModel() {
    private val addressRepository by lazy { AddressRepository() }
    private val allAddressOperate by lazy { MutableLiveData<Boolean>() }

    lateinit var name: InputBind
    lateinit var mobile: InputBind
    lateinit var addressSelect: InputBind
    lateinit var addressDetail: InputBind
    private lateinit var addressItem: AddressItemCEntity
    var defaultChecked: Boolean = false

    fun obsAllAddress() = allAddressOperate.switchMap { addressRepository.getAllAddress(it) }

    fun operateAllAddress(refresh: Boolean) {
        allAddressOperate.value = refresh
    }

    fun getSelect() = addressRepository.getSelect()

    fun updateSelect(addressItemCEntity: AddressItemCEntity) {
        AppDatabase.instant.runDataBaseTransition {
            getAddressDao().cleanSelect()
            getAddressDao().update(addressItemCEntity.also { it.isSelect = 1 })
        }
    }

    fun createAddressChangeBindModel(addressItem: AddressItemCEntity) {
        this.addressItem = addressItem
        val addressItemNoNull = addressItem.id.isNotEmpty()

        name = InputBind(hint = "收件人姓名", max = 10).apply {
            if (addressItemNoNull)
                input.set(addressItem.consignee)
        }

        mobile = InputBind(hint = "联系人手机号", inputType = InputType.TYPE_CLASS_NUMBER, max = 11).apply {
            if (addressItemNoNull)
                input.set(addressItem.mobile)
        }

        addressSelect = InputBind(hint = "收货地址 省市区", enable = false).apply {
            if (addressItemNoNull)
                input.set("${addressItem.province} ${addressItem.city} ${addressItem.district}")
        }

        addressDetail = InputBind(hint = "详细地址", max = 64).apply {
            if (addressItemNoNull)
                input.set(addressItem.address)
        }
        defaultChecked = addressItem.isDefault == 1
    }

    fun canCommit(): Boolean {
        if (name.input.get()?.length ?: 0 < 2) {
            name.error.set("收货人姓名最少2个字符")
            return false
        } else
            name.error.set(null)
        if (mobile.input.get()?.length ?: 0 != 11) {
            mobile.error.set("请输入正确的手机号码")
            return false
        } else
            mobile.error.set(null)
        if (addressItem.districtId.isEmpty()) {
            addressSelect.error.set("收货地址不能为空")
            return false
        } else
            addressSelect.error.set(null)
        if (addressDetail.input.get()?.length ?: 0 < 5) {
            addressDetail.error.set("详细地址最少5个字符")
            return false
        } else
            addressDetail.error.set(null)

        addressItem.consignee = name.input.get() ?: ""
        addressItem.mobile = mobile.input.get() ?: ""
        addressItem.address = addressDetail.input.get() ?: ""
        addressItem.isDefault = if (defaultChecked) 1 else 0
        return true
    }
}